{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Largest 3-Same-Digit Number in String"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: largestGoodInteger"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #字符串中最大的 3 位相同数字"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个字符串 <code>num</code> ，表示一个大整数。如果一个整数满足下述所有条件，则认为该整数是一个 <strong>优质整数</strong> ：</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>该整数是 <code>num</code> 的一个长度为 <code>3</code> 的 <strong>子字符串</strong> 。</li>\n",
    "\t<li>该整数由唯一一个数字重复 <code>3</code> 次组成。</li>\n",
    "</ul>\n",
    "\n",
    "<p>以字符串形式返回 <strong>最大的优质整数</strong> 。如果不存在满足要求的整数，则返回一个空字符串 <code>\"\"</code> 。</p>\n",
    "\n",
    "<p><strong>注意：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><strong>子字符串</strong> 是字符串中的一个连续字符序列。</li>\n",
    "\t<li><code>num</code> 或优质整数中可能存在 <strong>前导零</strong> 。</li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"6<em><strong>777</strong></em>133339\"\n",
    "<strong>输出：</strong>\"777\"\n",
    "<strong>解释：</strong>num 中存在两个优质整数：\"777\" 和 \"333\" 。\n",
    "\"777\" 是最大的那个，所以返回 \"777\" 。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"23<em><strong>000</strong></em>19\"\n",
    "<strong>输出：</strong>\"000\"\n",
    "<strong>解释：</strong>\"000\" 是唯一一个优质整数。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>num = \"42352338\"\n",
    "<strong>输出：</strong>\"\"\n",
    "<strong>解释：</strong>不存在长度为 3 且仅由一个唯一数字组成的整数。因此，不存在优质整数。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>3 &lt;= num.length &lt;= 1000</code></li>\n",
    "\t<li><code>num</code> 仅由数字（<code>0</code> - <code>9</code>）组成</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [largest-3-same-digit-number-in-string](https://leetcode.cn/problems/largest-3-same-digit-number-in-string/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [largest-3-same-digit-number-in-string](https://leetcode.cn/problems/largest-3-same-digit-number-in-string/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['\"6777133339\"', '\"2300019\"', '\"42352338\"']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "\n",
    "        result = '-1'\n",
    "        pos = 0\n",
    "        while pos + 3 <= len(num):\n",
    "            temp = num[pos:pos+3]\n",
    "            if len(set(temp)) == 1:\n",
    "                if int(temp) > int(result):\n",
    "                    result = temp\n",
    "            pos += 1\n",
    "        \n",
    "        if result == '-1':\n",
    "            return ''\n",
    "        else:\n",
    "            return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        lst=[]\n",
    "        for i in range(0,len(num)-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                lst.append(num[i])\n",
    "        lst.sort()\n",
    "        if len(lst)==0:\n",
    "            return \"\"\n",
    "        else:\n",
    "            return str(lst[-1])*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        c=''\n",
    "        for i in range(0,len(num)-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                c=max(c,num[i:i+3])\n",
    "        return c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        a=[\"999\",'888','777','666','555','444','333','222','111','000']\n",
    "        for i in a:\n",
    "            if i in num:\n",
    "                return i\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n=len(num)\n",
    "        ans=[]\n",
    "        i=0\n",
    "        while i<n-2:\n",
    "            if num[i]==num[i+1] and num[i+1]==num[i+2]:\n",
    "                ans+=[int(num[i])]\n",
    "                i+=3\n",
    "            else:\n",
    "                i+=1\n",
    "        if len(ans)==0:\n",
    "            return ''\n",
    "        else:\n",
    "            return str(max(ans))*3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        x = [str(x) for x in range(999, 0, -111)] + [\"000\"]\n",
    "        try:\n",
    "            return list(filter(lambda n: n in num, x))[0]\n",
    "        except IndexError:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        # 暴力,构造一个优质整数\n",
    "        # mx = -inf\n",
    "        # n = len(num)\n",
    "        # for v in num:\n",
    "        #     tm = v*3\n",
    "        #     if tm in num:\n",
    "        #         mx = max(mx, int(tm))\n",
    "        # if mx == 0:\n",
    "        #     return str(mx)*3\n",
    "        # elif mx == -inf:\n",
    "        #     return \"\"\n",
    "        # else:\n",
    "        #     return str(mx)\n",
    "\n",
    "\n",
    "        # 官解\n",
    "        n = len(num)\n",
    "        res = \"\"\n",
    "        for i in range(n-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                res = max(res, num[i:i+3])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 9\n",
    "        while i > 0:\n",
    "            s = str(i * 111) \n",
    "\n",
    "            if s in num:\n",
    "                return s\n",
    "\n",
    "            i -= 1\n",
    "        return (\"000\" in num) * \"000\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        now=\"\"\n",
    "        res=\"\"\n",
    "        con=0\n",
    "        for i in num:\n",
    "            if i==now:\n",
    "                con+=1\n",
    "                if con==3 and now>res:\n",
    "                    res=3*now\n",
    "            else:\n",
    "                now=i\n",
    "                con=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 9\n",
    "        while i > 0:\n",
    "            s = str(i * 111) \n",
    "            i -= 1\n",
    "            if s in num:\n",
    "                return s\n",
    "        return (\"000\" in num) * \"000\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        lis = []\n",
    "        n = len(num)\n",
    "        for i in range(n-2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                lis.append(num[i:i+3])\n",
    "        if lis:\n",
    "            return max(lis)\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        count = 0\n",
    "        ans =''\n",
    "        for i in range(len(num)-2):\n",
    "            if len(set(num[i:i+3])) == 1:\n",
    "                ans = max(ans, num[i:i+3])\n",
    "                if ans == '999':\n",
    "                    break\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 0\n",
    "        res = ''\n",
    "        while i+2 <len(num):\n",
    "            if num[i] == num[i+1] and num[i] == num[i+2]:\n",
    "                if res == '':\n",
    "                    res = num[i:i+3]\n",
    "                else:\n",
    "                    if int(num[i:i+3]) >int(res):\n",
    "                        res =num[i:i+3]\n",
    "            i += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res=\"0\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                if int(num[i])>=int(res[0]):\n",
    "                    res=num[i:i+3]\n",
    "        if res==\"0\":\n",
    "            return \"\"\n",
    "        return res\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        max_sub = -inf\n",
    "        for i in range(1, n-1):\n",
    "            if num[i] == num[i-1] and num[i] == num[i+1]:\n",
    "                max_sub = max(int(num[i-1: i+2]), max_sub)\n",
    "        max_sub = str(max_sub)\n",
    "        if  max_sub == '0':\n",
    "            max_sub = '000'  \n",
    "        return max_sub if max_sub != '-inf' else ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res = []\n",
    "        i = 0\n",
    "        while i < len(num) - 2:\n",
    "            r_two = num[i+1] == num[i+2]\n",
    "            if num[i] == num[i+1] and r_two:\n",
    "                res.append(num[i:i+3])\n",
    "                i += 3\n",
    "            elif r_two:\n",
    "                i += 1\n",
    "            else:\n",
    "                i += 2\n",
    "        if res:\n",
    "            res.sort()\n",
    "            return res[-1]\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                res = max(res, num[i:i+3])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = ''\n",
    "        cnt = 1\n",
    "        for i in range(1, len(num)):\n",
    "            if num[i] == num[i - 1]:\n",
    "                cnt += 1\n",
    "                if cnt == 3:\n",
    "                    ans = max(ans, num[i] * 3)\n",
    "            else:\n",
    "                cnt = 1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 0\n",
    "        _max = \"\"\n",
    "        while i <= len(num) - 3:\n",
    "            result = num[i]\n",
    "            for j in range(1,3):\n",
    "                if result != num[i+j]:\n",
    "                    break\n",
    "                if j == 2:\n",
    "                    if result * 3 > _max:\n",
    "                        _max = result * 3\n",
    "            i += 1\n",
    "        return _max\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        _max = 0\n",
    "        _res = \"\"\n",
    "        for i in range(1, len(num)-1):\n",
    "            if num[i-1] == num[i] == num[i+1]:\n",
    "                temp = num[i-1:i+2]\n",
    "                _max = max(_max, int(temp))\n",
    "                if _max == int(temp):\n",
    "                    _res = temp\n",
    "        return _res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        lst = ['999', '888', '777', '666', '555', '444', '333', '222', '111', '000']\n",
    "        for s in lst:\n",
    "            if s in num:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = ''\n",
    "        for i in range(2, len(num)):\n",
    "            if num[i] == num[i - 1] == num[i - 2]:\n",
    "                ans = max(ans, num[i] * 3)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 0\n",
    "        _max = []\n",
    "        while i <= len(num) - 3:\n",
    "            result = num[i]\n",
    "            for j in range(1,3):\n",
    "                if result != num[i+j]:\n",
    "                    break\n",
    "                if j == 2:\n",
    "                    _max.append(result * 3)\n",
    "            i += 1\n",
    "        if _max == []:\n",
    "            return \"\"\n",
    "        return max(_max)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "      temp = re.split('',num)\n",
    "      temp.pop(0)\n",
    "      temp.pop()\n",
    "      temp = list(set(temp))\n",
    "      temp.sort(reverse=True)\n",
    "      for i in temp:\n",
    "        result = i*3\n",
    "        if result in num:\n",
    "          return result\n",
    "          break\n",
    "      return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "\n",
    "        result = '-1'\n",
    "        pos = 0\n",
    "        while pos + 3 <= len(num):\n",
    "            temp = num[pos:pos+3]\n",
    "            if len(set(temp)) == 1:\n",
    "                if int(temp) > int(result):\n",
    "                    result = temp\n",
    "            pos += 1\n",
    "        \n",
    "        if result == '-1':\n",
    "            return ''\n",
    "        else:\n",
    "            return result\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        count = 0\n",
    "        ans =''\n",
    "        for i in range(1, len(num)):\n",
    "            if num[i] == num[i-1]:\n",
    "                count += 1\n",
    "                if count == 2:\n",
    "                    if num[i] == '9':\n",
    "                        return '999'\n",
    "                    ans = max(ans, num[i])\n",
    "                    count = 0\n",
    "            else:\n",
    "                count = 0\n",
    "        return ans * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        if \"999\" in num:\n",
    "            return \"999\"\n",
    "        if \"888\" in num:\n",
    "            return \"888\"\n",
    "        if \"777\" in num:\n",
    "            return \"777\"\n",
    "        if \"666\" in num:\n",
    "            return \"666\"\n",
    "        if \"555\" in num:\n",
    "            return \"555\"\n",
    "        if \"444\" in num:\n",
    "            return \"444\"\n",
    "        if \"333\" in num:\n",
    "            return \"333\"\n",
    "        if \"222\" in num:\n",
    "            return \"222\"\n",
    "        if \"111\" in num:\n",
    "            return \"111\"\n",
    "        if \"000\" in num:\n",
    "            return \"000\"    \n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(len(num)):\n",
    "            if num[i:i+3] == num[i] * 3:\n",
    "                ans = max(ans, num[i] * 3)\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, s: str) -> str:\n",
    "        z = ''\n",
    "        for i in range(3, len(s) + 1):\n",
    "            if s[i - 3] == s[i - 2] == s[i - 1]:\n",
    "                z = max(z, s[i-3:i])\n",
    "        return z\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        for i in range(9, -1, -1):\n",
    "            if str(i) * 3 in num:\n",
    "                return str(i) * 3\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        \n",
    "        ans = \"\"\n",
    "\n",
    "        for i in range(9,-1,-1):\n",
    "            s = str(i) + str(i) + str(i)\n",
    "            if s in num:\n",
    "                return s\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        \n",
    "        ans = \"\"\n",
    "\n",
    "        for i in range(9,-1,-1):\n",
    "            s = str(i) + str(i) + str(i)\n",
    "            if s in num:\n",
    "                return s\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        l=0\n",
    "        n=''\n",
    "        a=[]\n",
    "        for i in num:\n",
    "            if not n:\n",
    "                n+=i\n",
    "                l+=1\n",
    "            else:\n",
    "                if n[-1]==i and l<3:\n",
    "                    l+=1\n",
    "                    n+=i\n",
    "                elif n[-1]!=i:\n",
    "                    l=1\n",
    "                    n=i\n",
    "                if l==3:\n",
    "                    l=0\n",
    "                    a.append(n)\n",
    "                    n=''\n",
    "        if a:\n",
    "            a.sort(key=lambda x:int(x))\n",
    "            return a[-1]\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# class Solution:\n",
    "#     def largestGoodInteger(self, num: str) -> str:\n",
    "#         ans=\"\"\n",
    "#         for i in range(len(num)-2):\n",
    "#             if num[i:i+3]==num[i]*3:\n",
    "#                 # ans=max(ans,int(num[i:i+3])) ### 测例\"2300019\"\n",
    "#                 if not ans or int(num[i:i+3])>int(ans):\n",
    "#                     ans=num[i:i+3]\n",
    "#         return ans #str(ans) if ans>=0 else \"\"\n",
    "\n",
    "### 官码：字符串直接比较大小\n",
    "class Solution:\n",
    "    def largestGoodInteger(self,num):\n",
    "        n=len(num)\n",
    "        res=''\n",
    "        for i in range(n-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                res=max(res,num[i:i+3])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i]==num[i+1] and num[i+1]==num[i+2]:\n",
    "                res = max(res,num[i:i+3])\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "R = ['999', '888', '777', '666', '555', '444', '333', '222', '111', '000']\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "\n",
    "        return next(filter(lambda r: r in num, R), '')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n=[]\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i]==num[i+1]==num[i+2]:\n",
    "                n.append(int(num[i:i+3]))\n",
    "        re=str(max(n,default=''))\n",
    "        if re=='0':return '000'\n",
    "        return re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        num=[i for i in num]\n",
    "        list1=[]\n",
    "        for i in range(1,len(num)-1):\n",
    "            if num[i]==num[i-1] and num[i]==num[i+1]:\n",
    "                list1.append(int(num[i]*3))\n",
    "        if list1==[]:\n",
    "            return ''\n",
    "        elif max(list1)==0:\n",
    "            return '000'\n",
    "        else:\n",
    "            return str(max(list1))\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res = '0'\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i] == num[i+1] == num[i+2] and int(num[i:i+3]) >= int(res):\n",
    "                res = num[i:i+3]\n",
    "        return res if res != '0' else ''             \n",
    "                \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        key=-1\n",
    "        res=\"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if int(num[i])>key:\n",
    "                if num[i]==num[i+1]==num[i+2]:\n",
    "                    key=int(num[i])\n",
    "                    res=num[i:i+3]\n",
    "        return res\n",
    "\n",
    "            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        count = 0\n",
    "        ans =''\n",
    "        for i in range(len(num)-2):\n",
    "            if len(set(num[i:i+3])) == 1:\n",
    "                ans = max(ans, num[i:i+3])\n",
    "        return ans\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res = \"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                res = max(res,num[i:i+3])\n",
    "        return res\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/largest-3-same-digit-number-in-string/solutions/1538493/zi-fu-chuan-zhong-zui-da-de-3-wei-xiang-isykz/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        for i in range(9, -1, -1):\n",
    "            if (s := str(i) * 3) in num:\n",
    "                return s\n",
    "        return ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ret = -1\n",
    "        for i in range(2, len(num)):\n",
    "            if num[i] == num[i - 1] and num[i] == num[i - 2]:\n",
    "                v = int(num[i])\n",
    "                if v > ret:\n",
    "                    ret = v\n",
    "        if ret >= 0:\n",
    "            return str(ret) * 3\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        k, n, ans = 3, len(num), []\n",
    "        for i in range(n - 2):\n",
    "            sk = num[i:i + 3]\n",
    "            if num[i] == num[i + 1] == num[i + 2]:\n",
    "                ans.append(sk)\n",
    "        if ans:\n",
    "            return max(ans)\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        max_sub = -inf\n",
    "        for i in range(1, n-1):\n",
    "            if num[i] == num[i-1] and num[i] == num[i+1]:\n",
    "                max_sub = max(int(num[i-1: i+2]), max_sub)\n",
    "        max_sub = str(max_sub)\n",
    "        if  max_sub == '0':\n",
    "            max_sub = '000'  \n",
    "        return max_sub if max_sub != '-inf' else ''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        res = \"\"\n",
    "        for i in range(n - 2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                res = max(res, num[i:i+3])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(0, len(num) - 2):\n",
    "            if num[i] == num[i + 1] and num[i] == num[i + 2] and num[i :i+3] > ans:\n",
    "                ans = num[i: i + 3]\n",
    "        return ans\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        l=r=0\n",
    "        result=''\n",
    "        while r<len(num):\n",
    "            if r-l>2:\n",
    "                if result!='' and result[0]<num[l]:\n",
    "                    result=num[l]+num[l]+num[l]\n",
    "                elif result=='':\n",
    "                    result=num[l]+num[l]+num[l]\n",
    "                else:\n",
    "                    l=r\n",
    "            if r>=len(num):\n",
    "                break\n",
    "            if num[l]==num[r]:\n",
    "                r+=1\n",
    "            else:\n",
    "                l=r\n",
    "                r=l\n",
    "        if r-l>2:\n",
    "            if result!='' and result[0]<num[l]:\n",
    "                result=num[l]+num[l]+num[l]\n",
    "            elif result=='':\n",
    "                result=num[l]+num[l]+num[l]\n",
    "        return result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(1, len(num) - 1):\n",
    "            if num[i - 1] == num[i] and num[i + 1] == num[i]:\n",
    "                ans = max(ans, num[i - 1: i + 2])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = \"\"\n",
    "        for i in range(len(num) - 2):\n",
    "            if num[i] == num[i + 1] == num[i + 2]:\n",
    "                if ans:\n",
    "                    if int(num[i: i + 3]) > int(ans):\n",
    "                        ans = num[i: i + 3]\n",
    "                else:\n",
    "                    ans = num[i: i + 3]\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        #固定滑窗\n",
    "        n = len(num)\n",
    "        right = 0\n",
    "        cur = ''\n",
    "        res = -1\n",
    "        while right < n:\n",
    "            if right >= 2 and int(num[right-2:right+1]) % 111 == 0:\n",
    "                res = max(res,int(num[right-2:right+1]))\n",
    "            right += 1\n",
    "        if res == 0:\n",
    "            return \"000\"\n",
    "        if res == -1:\n",
    "            return \"\"\n",
    "        return str(res)\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        lst = [\"000\", \"111\", \"222\", \"333\", \"444\", \"555\", \"666\", \"777\", \"888\", \"999\"]\n",
    "        for s in lst[::-1]:\n",
    "            if s in num:\n",
    "                return s\n",
    "        return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        res = \"\"\n",
    "        for i in range(n - 2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                res = max(res, num[i:i+3])\n",
    "        return res\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        result_list = []\n",
    "        if len(num) <3 or (len(num)==3 and num[0] != num[1] and num [0]!=num[2]):\n",
    "            return \"\"\n",
    "        elif len(num) ==3 and num[0] == num[2] and num[0] == num[1]:\n",
    "            return num\n",
    "        else:\n",
    "            num = num + 'x'\n",
    "            for i in range(0,len(num)-3):\n",
    "                if (num[i] == num[i+2] and num[i] == num[i+1] and num[i] != num[i+3]):\n",
    "                    result_list.append(num[i:i+3])\n",
    "            if len(result_list)>0:\n",
    "                return max(result_list)\n",
    "        \n",
    "            else:\n",
    "                return \"\"\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "      nums = num\n",
    "      max_ = float('-inf')\n",
    "      for i in range(len(nums) - 2):\n",
    "        if nums[i] == nums[i + 1] == nums[i + 2]:\n",
    "          max_ = max(max_, int(nums[i:i + 3]))\n",
    "      \n",
    "\n",
    "      if max_ == float('-inf'):\n",
    "        return ''\n",
    "      elif max_ == 0:\n",
    "        return '000'\n",
    "      else:\n",
    "        return str(max_)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans = -1\n",
    "        num=str(num)\n",
    "        n = len(num)\n",
    "        cnt = 1\n",
    "        for idx in range(n - 1):\n",
    "            if num[idx] == num[idx + 1]:\n",
    "                cnt += 1\n",
    "                if cnt == 3:\n",
    "                    ans = max(ans, ord(num[idx]))\n",
    "            else:\n",
    "                cnt = 1\n",
    "\n",
    "        return \"\" if ans == -1 else str(ans - ord('0'))*3\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        result = \"000\"\n",
    "        p = False\n",
    "        for i in range(len(num)-1, 1, -1):\n",
    "            if num[i] == num[i-1] and num[i-1] == num[i-2] and num[i] >= result[0]:\n",
    "                p = True\n",
    "                result = num[i-2:i+1]\n",
    "        if p == False:\n",
    "            result = \"\"\n",
    "        return result\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res=[]\n",
    "        nums=list(num)\n",
    "        for i in range(2,len(nums)):\n",
    "            if nums[i]==nums[i-1]==nums[i-2]:\n",
    "                res.append(''.join(nums[i-2:i+1]))\n",
    "            \n",
    "        if res:\n",
    "            \n",
    "            return max(res)\n",
    "        return \"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        L=set()\n",
    "        for i in range(0,len(num)-2):\n",
    "            if num[i] not in L and num[i]==num[i+1] and num[i]==num[i+2]:\n",
    "                L=L|{num[i]}\n",
    "        if L==set():\n",
    "            return \"\"\n",
    "        return max(L)+max(L)+max(L)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        max1 = -1\n",
    "        for i in range(2, len(num)):\n",
    "            if num[i-2:i+1] == num[i]*3:\n",
    "                max1 = max(int(num[i-2:i+1]), max1)\n",
    "        if max1==-1:\n",
    "            return ''\n",
    "        else:\n",
    "            return '000' if max1==0 else str(max1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        max1 = -1\n",
    "        for i in range(2, len(num)):\n",
    "            if num[i-2:i+1] == num[i]*3:\n",
    "                max1 = max(int(num[i-2:i+1]), max1)\n",
    "        if max1==-1:\n",
    "            return ''\n",
    "        else:\n",
    "            return '000' if max1==0 else str(max1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        ans=\"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i:i+3]==num[i]*3:\n",
    "                # ans=max(ans,int(num[i:i+3])) ### 测例\"2300019\"\n",
    "                if not ans or int(num[i:i+3])>int(ans):\n",
    "                    ans=num[i:i+3]\n",
    "        return ans #str(ans) if ans>=0 else \"\"\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        r = '000'\n",
    "        flag = False\n",
    "        for i in range(10):\n",
    "            a = str(i)*3\n",
    "            res = re.findall(a,num)\n",
    "            if len(res)>=1:\n",
    "                if res[0][0]>=r[0]:\n",
    "                    r = res[0]\n",
    "                    flag = True\n",
    "        if flag==False:\n",
    "            return \"\"\n",
    "        return r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        i = 0\n",
    "        _max = []\n",
    "        while i <= len(num) - 3:\n",
    "            result = num[i]\n",
    "            for j in range(1,3):\n",
    "                if result != num[i+j]:\n",
    "                    break\n",
    "                if j == 2:\n",
    "                    _max.append(result * 3)\n",
    "            i += 1\n",
    "        if _max == []:\n",
    "            return \"\"\n",
    "        return max(_max)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        res = \"\"\n",
    "        for i in range(n-2) :\n",
    "            if num[i] == num[i+1] == num[i+2] :\n",
    "                res = max(res , num[i:i+3])\n",
    "        return res "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution(object):\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        res = \"\"\n",
    "        for i in range(n-2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                res = max(res,num[i:i+3])\n",
    "        return res\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/largest-3-same-digit-number-in-string/solutions/1538493/zi-fu-chuan-zhong-zui-da-de-3-wei-xiang-isykz/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        fh = \"\"\n",
    "        for i in range(len(num)-2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                fh = max(fh, num[i:i+3])\n",
    "        return fh"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        n = len(num)\n",
    "        ans = \"\"\n",
    "        for i in range(n - 2):\n",
    "            if num[i] == num[i+1] == num[i+2]:\n",
    "                ans = max(ans, num[i:i+3])\n",
    "        return ans\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        res=[]\n",
    "        for i in range(len(num)):\n",
    "            if num[i:i+3]==num[i]*3:\n",
    "                res.append(num[i:i+3])\n",
    "        res.sort(key=lambda x:(int(x)))\n",
    "        if res:\n",
    "            return res[-1]\n",
    "        else:\n",
    "            return \"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def largestGoodInteger(self, num: str) -> str:\n",
    "        for i in range(9, -1, -1):\n",
    "            if str(i) * 3 in num:\n",
    "                return str(i) * 3\n",
    "        return ''"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
